<?php

/**
 * Albireo Kernel
 *
 * @copyright  Copyright (c) 2010 Albireo Solutions
 * @package    Kernel
 */

namespace Albireo\OAuthServer;

use \dibi;
use Nette\Object;

class Token extends Object implements IToken{

    protected $id;
    protected $type;
    protected $consumer;
    protected $token;
    protected $token_secret;
    protected $callback;
    protected $verifier;
    protected $user;
    protected $pdo;

    /* static functions */

    public static function createRequestToken(IConsumer $consumer, $token, $tokensecret, $callback) {
        dibi::query("insert into app_token (type,app_id,token,token_secret,callback_url) values (1," . $consumer->getId() . ",'" . $token . "','" . $tokensecret . "','" . $callback . "') ");
    }

    public static function findByToken($token) {
        $ret = null;
        $find = dibi::query("select token_id from app_token where token = '" . $token . "'");
        if ($find->rowCount() == 1) {
            $find = $find->fetch();
            $ret = new Token($find['token_id']);
        }
        return $ret;
    }

    public function __construct($id=0) {
        if ($id != 0) {
            $this->id = $id;
            $this->load();
        }
    }

    private function load() {
        $info = dibi::fetch("select * from app_token where token_id = " . $this->id);
        $this->token = $info['token'];
        $this->type = $info['type'];
        $this->token_secret = $info['token_secret'];
        $this->consumer = new Consumer($info['app_id']);
        $this->callback = $info['callback_url'];
        $this->verifier = $info['verifier'];
        if ($info['user_id'] != 0) {
            $this->user = new User($info['user_id']);
        } else {
            $this->user = 0;
        }
    }

    public function changeToAccessToken($token, $secret) {
        if ($this->isRequest()) {
            dibi::query("update app_token set type = 2, verifier = '', callback_url = '', token = '" . $token . "', token_secret = '" . $secret . "' where token_id = " . $this->id);
            return true;
        } else {
            return false;
        }
    }

    /* some setters */

    public function setVerifier($verifier) {
        dibi::query("update app_token set verifier = '" . $verifier . "' where id = " . $this->id);
        $this->verifier = $verifier;
    }

    public function setUser(IUser $user) {
        $this->pdo->exec("update token set user_id = '" . $user->getId() . "' where token_id = " . $this->id);
        $this->user = $user;
    }

    /* some getters */

    public function isRequest() {
        return $this->type == 1;
    }

    public function isAccess() {
        return !$this->isRequest();
    }

    public function getCallback() {
        return $this->callback;
    }

    public function getVerifier() {
        return $this->verifier;
    }

    public function getType() {
        return $this->type;
    }

    public function getSecret() {
        return $this->token_secret;
    }

    public function getUser() {
        return $this->user;
    }

}